/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import cs4280.bean.Movie;
import cs4280.bean.Screening;
import cs4280.bean.Screening;
import cs4280.dao.BookingDAO;
import cs4280.dao.MovieDAO;
import cs4280.dao.ScreeningDAO;
import cs4280.dao.ScreeningDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
 *
 * @author Yvonne
 */
public class screeningControl extends HttpServlet {


    public static enum RESULT {

        OK, DUPLICATE, FAIL
    };


    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet screeningControl</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet screeningControl at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    public void setMovieNames(ArrayList<Screening> screenings) {
        MovieDAO movieDAO = new MovieDAO();
        String moviename;
        Screening screening;
        Iterator<Screening> it = screenings.iterator();
        while (it.hasNext() != false) {
            screening = (Screening) it.next();
            moviename = movieDAO.getMoviename(screening.getMovieid());
            screening.setMoviename(moviename);
        }
    }

    public ArrayList<Screening> getScreenings() {
        ArrayList<Screening> screenings = new ArrayList<Screening>();

        ScreeningDAO screeningDAO = new ScreeningDAO();
        screenings = screeningDAO.getScreenings();
        this.setMovieNames(screenings);
        return screenings;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    public String doManageScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Movie movie = new Movie();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("screenings", screeningDAO.getOneMovieScreenings(movie.getId()));
        request.setAttribute("movie", movie);
        return "/WEB-INF/jsp/screeningManager.jsp";
    }

    public String doPrepareUpdateScreeningPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));

        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid")));
        BookingDAO bookingDAO = new BookingDAO();

        request.setAttribute("screening", screening);
        request.setAttribute("movie", movie);
        return "/WEB-INF/jsp/screeningUpdater.jsp";

    }

    public String doUpdateScreeningPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Screening screening = new Screening();
        screening.setId(Integer.parseInt(request.getParameter("screeningid")));

        screening.setShowtime(request.getParameter("showtime"));
        screening.setHouseid(Integer.parseInt(request.getParameter("houseid")));
        screening.setPrice(Integer.parseInt(request.getParameter("price")));
        request.setAttribute("screening", screening);

        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);


        switch (screeningDAO.updateScreening(screening)) {

            case OK:
                request.setAttribute("msg", "Update screening successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to update screening");
        }
        return "/WEB-INF/jsp/doscreeningUpdater.jsp";

    }


    public String doPrepareDeleteScreeningPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Screening screening = new Screening();
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));

        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid")));
        request.setAttribute("screening", screening);
        request.setAttribute("movie", movie);
        return "/WEB-INF/jsp/screeningDeleter.jsp";

    }

    public String doDeleteScreeningPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ScreeningDAO screeningDAO = new ScreeningDAO();
        Screening screening = new Screening();
        screening = screeningDAO.getScreening(Integer.parseInt(request.getParameter("screeningid"))) ;

       
        request.setAttribute("screening", screening);

        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);


        switch (screeningDAO.deleteScreening(screening)) {

            case OK:
                request.setAttribute("msg", "Delete screening successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to delete screening");
        }
        return "/WEB-INF/jsp/doscreeningDeleter.jsp";

    }

    public String doPrepareAddScreeningPage(HttpServletRequest request, HttpServletResponse response) {
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie=movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);

        return "/WEB-INF/jsp/screeningAdder.jsp";
    }

    public String doAddScreeningPage(HttpServletRequest request, HttpServletResponse response) {
        Screening screening = new Screening();
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));

        ScreeningDAO screeningDAO = new ScreeningDAO();
        screening.setMoviename(request.getParameter("moviename"));
        screening.setMovieid(movie.getId());
        screening.setShowtime(request.getParameter("showtime"));
        screening.setHouseid(Integer.parseInt(request.getParameter("houseid")));
        screening.setPrice(Integer.parseInt(request.getParameter("price")));


        request.setAttribute("movie", movie);
        request.setAttribute("screening", screening);


        switch (screeningDAO.addScreening(movie,screening)) {

        case OK:
        request.setAttribute("msg", "Add screening successfully!");
        break;
        case DUPLICATE:
        request.setAttribute("msg", "Screening already exists!");
        break;
        case FAIL:
        request.setAttribute("msg", "Fail to add screening");
        }

        return "/WEB-INF/jsp/doscreeningAdder.jsp";
    }
}
